const BSER_URL = 'https://api-hmugo-web.itheima.net/api/public/v1'

let ajaxTimes = 0;
export const request = (params) => {
    let header={...params.header}
    if(params.url.includes('/my/')){
        header['Authorization']=wx.getStorageSync('token')
    }
    ajaxTimes++
    wx.showLoading({
        title: '加载中',
    })
    return new Promise((resolve, reject) => {
        wx.request({
            url: BSER_URL + params.url,
            method: params.method || 'GET',
            data: params.data || {},
            header:header,
            success: res => {
                // if (res.data.meta.status != 200) {
                //     return wx.showToast({
                //         title: '获取数据失败',
                //         icon: 'none',
                //         duration: 2000
                //     })
                // }
                resolve(res.data.message)
            },
            fail: err => {
                wx.showToast({
                    title: '请求接口失败',
                    icon: 'none',
                    duration: 2000
                })
                reject(err)
            },
            complete: () => {
                ajaxTimes--
                if (ajaxTimes === 0) {
                    wx.hideLoading()
                }
            }
        });
     
    })
}